# -*- coding: utf-8 -*- 
"""
Module Description: 区服开关
Time : 2020/5/25 10:26 
Author : Wang P
File : server_switch_utils.py
version: 
"""
from pyshare.dal.repository_impl import CenterModuleServerSwitchRepository, ActivityStatus


class ServerSwitch(object):

    def __init__(self):
        self.repository = CenterModuleServerSwitchRepository

    def filter_server_switch(self):
        return self.repository.filter_server_switch()

    def get_server_bus_switch(self, module_id):
        """
        获取区服开关
        :param module_id:
        :return:
        """
        module_record = self.repository.get(module_id)
        if not module_record:
            module_record = self.add_server_bus_switch(module_id)
        return module_record

    def add_server_bus_switch(self, module_id):
        """
        添加区服开关
        :param module_id:
        :return:
        """
        return self.repository.create(module_id, ActivityStatus.Close, [])

    def save(self, module_switch_record):
        """
        保存区服开关
        :param module_switch_record:
        :return:
        """
        self.repository.save(module_switch_record)

    def check_server_is_open(self, module_id, server_id):
        """
        检查区服开发是否开启
        :param module_id:
        :param server_id:
        :return:
        """
        module_record = self.get_server_bus_switch(module_id)
        if not module_record:
            return False
        if module_record.open_status != ActivityStatus.Open:
            return False
        if server_id in module_record.close_server_list:
            return False
        return True

